const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personalLabelListIndex: -1,
    areaList: '',
    type:'',
    // 地区信息
    id:'',
    subid:'',
    aresname:'',
    aressubname:'',
    // 任务信息
    taskid:'',
    tasktitle:'',
    tasksubtitle:'',
    // 增值服务
    serviceList:[],
    serviceTextList:[],
    serviceIdList:[],
    servicePrice:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    if (options.type == 0){
      wx.setNavigationBarTitle({
        title: '选择标签'
      })
    } else if (options.type == 1){
      wx.setNavigationBarTitle({
        title: '选择地区'
      })
    }else{
      wx.setNavigationBarTitle({
        title: '选择增值服务'
      })
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      this.setData({
        serviceTextList:prevPage.data.serviceTextList
      })
    }

    this.labelList();
    // 增值服务
    this.serviceList();
  },
  // 标签列表
  labelList: function () {
    var { type } = this.data
    var _this = this;
    if (type == 0){
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseUrl + 'taskCate',
        data: {},
        success(res) {
          wx.hideLoading()
          if (res.data.code == 1000) {
            _this.setData({
              areaList: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    } else if (type == 1){
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: app.globalData.baseUrl + 'task/district',
        data: {},
        success(res) {
          wx.hideLoading()
          if (res.data.code == 1000) {
            _this.setData({
              areaList: res.data.data
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  // 增值服务
  serviceList:function(){
    var _this = this;
    var { serviceList,serviceTextList } = this.data
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'myTask/gettaskService',
      data: {
        token: app.globalData.token
      },
      success(res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 1000) {
          var obj = res.data.data
          serviceList = res.data.data
          for (var i = 0; i < obj.length; i++) {
            serviceList[i].type = false
          }
          if(serviceTextList != ''){
            for (var i = 0; i < serviceTextList.length; i++) {
              for (var j = 0; j < serviceList.length; j++) {
                if(serviceList[j].title == serviceTextList[i]){
                  serviceList[j].type = true
                }
              }
            }
          }
          console.log(serviceList)
          _this.setData({
            serviceList
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 选择标签
  areaLabelList: function (e) {
    var { type } = this.data;
    var { id, subid, aresname, aressubname } = e.currentTarget.dataset;
    if(type == 0){
      this.setData({
        personalLabelListIndex: subid,
        subid: subid,
        aresname: aresname,
        aressubname: aressubname
      })
    }else{
      this.setData({
        personalLabelListIndex: subid,
        id: id,
        subid: subid,
        aresname: aresname,
        aressubname: aressubname
      })
    }
  },
  // 选择增值服务
  serviceListBtn:function(e){
    var { serviceList, serviceIdList, serviceTextList, servicePrice } = this.data
    var { index, serviceid, servicename } = e.currentTarget.dataset
    var servicePrice = '0';
    serviceList[index].type = !serviceList[index].type
    serviceIdList = [];
    serviceTextList = [];
    for (var i = 0; i < serviceList.length; i++){
      if (serviceList[i].type){
        servicePrice = parseInt(servicePrice) + parseInt(serviceList[i].price)
        serviceIdList.push(serviceList[i].id);
        serviceTextList.push(serviceList[i].title)
      }
    }
    this.setData({
      serviceList,
      serviceIdList,
      serviceTextList,
      servicePrice
    })
  },
  // 确定
  labelSave:function(){
    var { id, subid, aresname, aressubname,type } = this.data;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    if(type != 2){
      if (subid == '') {
        wx.showToast({
          title: '您还未选择地区！',
          icon: 'none',
          duration: 2000
        })
        return false
      }
      if (type == 0) {
        prevPage.setData({
          taskid: subid,
          tasktitle: aresname,
          tasksubtitle: aressubname
        })
      } else {
        prevPage.setData({
          id: id,
          subid: subid,
          aresname: aresname,
          aressubname: aressubname
        })
      }
    }else{
      var { serviceIdList, serviceTextList, servicePrice } = this.data;
      console.log(serviceTextList)
      prevPage.setData({
        serviceIdList,
        serviceTextList,
        servicePrice
      })
    }
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
